package jjn.carl.backtrack;

import java.util.*;

/**
 * @author Jjn
 * @since 2023/7/29 13:26
 */
public class LeetCode491 {
    public List<List<Integer>> findSubsequences(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        backtrack(nums, result, 0, new ArrayList<>());
        return result;
    }
    
    private void backtrack(int[] nums, List<List<Integer>> result, int startIndex, List<Integer> path) {
        if (path.size() >= 2) {
            result.add(new ArrayList<>(path));
        }
        Set<Integer> set = new HashSet<>();
        for (int i = startIndex; i < nums.length; i++) {
            if (!path.isEmpty() && nums[i] < path.get(path.size() - 1) || set.contains(nums[i])) {
                continue;
            }
            path.add(nums[i]);
            set.add(nums[i]);
            backtrack(nums, result, i + 1, path);
            path.remove(path.size() - 1);
        }
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int totalNumber = scanner.nextInt();
        int[] nums = new int[totalNumber];
        for (int i = 0; i < totalNumber; i++) {
            nums[i] = scanner.nextInt();
        }
        List<List<Integer>> subsequences = new LeetCode491().findSubsequences(nums);
        System.out.println(String.join(",", subsequences.toString()));
    }
}
